<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs v-model="activeName">
          <!-- 登录账户设置 -->
          <el-tab-pane label="登录账户设置" name="first">
            <!-- 放置表单 -->
            <el-form
              ref="form"
              :rules="rulesForm"
              :model="formDate"
              label-width="100px"
              style="margin-left: 35%;margin-top:50px"
            >
              <el-form-item label="姓名" prop="username">
                <el-input
                  v-model="formDate.username"
                  style="width:300px"
                ></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="password2">
                <el-input
                  type="password"
                  v-model="formDate.password2"
                  style="width:300px"
                ></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUserDetail"
                  >更新</el-button
                >
                <el-button @click="btnclose">取消</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <!-- 个人详情部分 -->
          <el-tab-pane label="个人详情" name="second">
            <!-- 打印页面跳转 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印个人基本信息">
                <router-link :to="`/employees/print/${userId}?type=personal`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 组件部分 -->
            <!-- <user-info></user-info> -->
            <el-button type="primary" @click="UserComponent = 'el-button'"
              >切换组件</el-button
            >
            <el-button type="primary" @click="UserComponent = 'user-info'"
              >切换组件1</el-button
            >
            <component :is="UserComponent"></component>
          </el-tab-pane>

          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息" name="third">
            <!-- 打印页面跳转 -->
            <el-row type="flex" justify="end">
              <el-tooltip content="打印岗位信息">
                <router-link :to="`/employees/print/${userId}?type=job`">
                  <i class="el-icon-printer"></i>
                </router-link>
              </el-tooltip>
            </el-row>
            <!-- 组件部分 -->
            <component :is="(JobComponent = 'job-info')"></component>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
// 获取某个用户的信息
import { getUserProfile } from "@/api/user";
// 导入保存员工基本信息接口
import { saveUserDetailById } from "@/api/employees";
// 个人详情组件
import UserInfo from "./components/user-info.vue";
// 岗位信息组件
import JobInfo from "./components/job-info.vue";
export default {
  // 组件名称
  name: "Detaile",
  // 组件参数 接收来自父组件的数据
  props: {},
  // 局部注册的组件
  components: {
    UserInfo,
    JobInfo
  },
  // 组件状态值
  data() {
    return {
      UserComponent: "UserInfo",
      JobComponent: "job-info",
      userId: this.$route.params.id, // 定义在路由中的id
      activeName: "first",
      formDate: {
        username: "",
        password2: ""
      },
      rulesForm: {
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        password2: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          { min: 6, max: 12, trigger: "blur", message: "密码格式不正确" }
        ]
      }
    };
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    // 获取员工信息
    async getUserProfile() {
      this.formDate = await getUserProfile(this.userId);
    },
    // 保存员工基本信息
    async saveUserDetail() {
      try {
        //  获取表单实例对象，对表单进行校验
        await this.$refs.form.validate();
        // 把更改的密码覆盖掉新密码
        await saveUserDetailById({
          ...this.formDate,
          password: this.formDate.password2
        });

        this.$message.success("保存成功");
      } catch (error) {
        console.log(error);
      }
    },
    btnclose() {
      this.$refs.form.resetFields();
    }
  },
  // 以下是生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  created() {
    this.getUserProfile();
  },
  /**
   * el 被新创建的 vm.$ el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.$ el 也在文档内。
   */
  mounted() {}
};
</script>

<style scoped lang="less"></style>
